package com.dingpeikeji.quickdeer.modules.rentproduct.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.DemandsReact;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TDemands;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.DemandsPriceService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCargotypeService;

/**
 * 需求报价
 *
 */
@Controller
@RequestMapping(value = "${adminPath}/rentproduct/demandsPrice")
public class DemandsPriceController extends BaseController {

	@Autowired
	private DemandsPriceService demandsPriceService;
	@Autowired
	private TCargotypeService tCargotypeService;

	/**
	 * 需求报价列表
	 */
	@RequiresPermissions("rentproduct:demandsPrice:allview")
	@RequestMapping(value = { "list", "" })
	public String list(TDemands tDemands, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TDemands> page = demandsPriceService.findPage(new Page<TDemands>(request, response), tDemands);
		model.addAttribute("page", page);
		model.addAttribute("tDemands", tDemands);
		return "modules/rentproduct/demandsAllList";
	}
	
	@RequiresPermissions("rentproduct:demandsPrice:allview")
	@RequestMapping(value = { "allform" })
	public String allForm(TDemands tDemands, HttpServletRequest request, HttpServletResponse response, Model model) {
		tDemands = demandsPriceService.get(tDemands.getDemandid().toString());
		
		List<DemandsReact> reactList = demandsPriceService.findDemandsReactList(tDemands);
		model.addAttribute("reactList", reactList);

		// 车型数据
		List<TCargotype> cargotypeList = tCargotypeService.findList(new TCargotype());
		model.addAttribute("cargotypeList", cargotypeList);
		model.addAttribute("tDemands", tDemands);
		return "modules/rentproduct/demandsAllForm";
	}
	
	/**
	 * 需求报价列表
	 */
	@RequiresPermissions("rentproduct:demandsPrice:view")
	@RequestMapping(value = { "waitlist" })
	public String waitlist(TDemands tDemands, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TDemands> page = demandsPriceService.findWaitList(new Page<TDemands>(request, response), tDemands);
		model.addAttribute("page", page);
		model.addAttribute("tDemands", tDemands);
		return "modules/rentproduct/demandsPriceList";
	}
	
	/**
	 * 本司已处理报价列表需求报价列表
	 */
	@RequiresPermissions("rentproduct:demandsPrice:view")
	@RequestMapping(value = { "busreactlist" })
	public String busreactlist(DemandsReact tReact, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DemandsReact> page = demandsPriceService.findBusReactList(new Page<DemandsReact>(request, response), tReact);
		model.addAttribute("page", page);
		model.addAttribute("tReact", tReact);
		return "modules/rentproduct/demandsReactList";
	}
	
	/**
	 * 查看需求
	 */
	@RequiresPermissions("rentproduct:demandsPrice:view")
	@RequestMapping(value = "form")
	public String form(TDemands tDemands, Model model) {
		tDemands = demandsPriceService.get(tDemands.getDemandid().toString());

		// 车型数据
		List<TCargotype> cargotypeList = tCargotypeService.findList(new TCargotype());
		model.addAttribute("cargotypeList", cargotypeList);
		model.addAttribute("tDemands", tDemands);
		return "modules/rentproduct/demandsPriceForm";
	}

	/**
	 * 反馈
	 */
	@RequiresPermissions("rentproduct:demandsPrice:edit")
	@RequestMapping(value = "save")
	public String save(DemandsReact demandsReact, HttpServletRequest request,RedirectAttributes redirectAttributes) {
/*		if (demandsReact.getCartype() == null || demandsReact.getSeats() == null) {
			redirectAttributes.addFlashAttribute("message", "车型信息不全");
			return "redirect:" + Global.getAdminPath() + "/rentproduct/demandsPrice/form?demandid="
					+ demandsReact.getDemandid();
		}*/
		
		try {
			request.setCharacterEncoding("utf-8");
			if (demandsPriceService.feedback(demandsReact) ) {		    
				redirectAttributes.addFlashAttribute("message", "反馈成功");
			} else {
				redirectAttributes.addFlashAttribute("message", "已被其他公司抢先一步");
			}
		} catch (Exception e) {
			redirectAttributes.addFlashAttribute("message", "数据库繁忙，请稍后再试");
		}
		if(demandsPriceService.sendMessage(demandsReact)){
			System.out.println("发送成功");
		}
		return "redirect:" + Global.getAdminPath() + "/rentproduct/demandsPrice/waitlist?repage";
	}

}
